package com.fwtai.api.controller;

import com.fwtai.bean.PageFormData;
import com.fwtai.config.ConfigFile;
import com.fwtai.service.api.ApiUserService;
import com.fwtai.tool.ToolClient;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.concurrent.CompletableFuture;

/**
 * 微信登录功能,可以实现注册并登录的效果
 * @作者 田应平
 * @版本 v1.0
 * @创建时间 2021/12/29 12:29
 * @QQ号码 444141300
 * @Email service@yinlz.com
 * @官网 <url>http://www.fwtai.com</url>
*/
@Api(tags = "小程序授权或登录,token不填",description = "微信小程序操作")
@RestController
@RequestMapping(ConfigFile.guest_v10 + "wechat")
public class WechatController{

  @Autowired
  private ApiUserService apiUserService;

  @Resource
  private ThreadPoolTaskExecutor taskExecutor;

  @ApiOperation("通过微信的code登录获取微信信息的openId和session_key,获取微信数据信息")
  @ApiImplicitParams({
    @ApiImplicitParam(name = "code", value = "微信的code", dataType = "String", paramType = "query", required = true)
  })
  @PostMapping("/getInfo")
  public void getInfo(final String code,final HttpServletResponse response){
    ToolClient.responseJson(apiUserService.getInfo(code),response);
  }

  @ApiOperation("微信登录方式,若返回的code为198时说明该'微信号'尚未绑定手机号,此时仅返回openid,跳转到绑定手机号码或完善个人资料,由用户选择绑定手机号码或完善个人资料,推荐使用!")
  @ApiImplicitParams({
    @ApiImplicitParam(name = "code", value = "微信的code", dataType = "String", paramType = "query", required = true)
  })
  @PostMapping("/login")
  public void login(final String code,final HttpServletResponse response){
    final CompletableFuture<String> async = CompletableFuture.supplyAsync(() -> apiUserService.login(code),taskExecutor);
    ToolClient.responseJson(async.join(),response);
  }

  @ApiOperation("注册并登录,通过微信登录时返回code=198时跳转到完善个人资料页面[说明该用户首次使用]-简化注册或登录")
  @ApiImplicitParams({
    @ApiImplicitParam(name = "codeOpenId", value = "获取openId的code", dataType = "String", paramType = "query", required = true),
    @ApiImplicitParam(name = "codePhone", value = "获取微信用户绑定的手机号的code", dataType = "String", paramType = "query", required = true)
  })
  @PostMapping("/signIn")
  public void signIn(final HttpServletRequest request,final HttpServletResponse response){
    ToolClient.responseJson(CompletableFuture.supplyAsync(() -> apiUserService.signIn(request),taskExecutor).join(),response);
  }

  @ApiOperation("通过微信的code获取微信绑定的手机号")
  @ApiImplicitParams({
    @ApiImplicitParam(name = "code", value = "微信的code", dataType = "String", paramType = "query", required = true)
  })
  @PostMapping("/getWechatPhone")
  public void getWechatPhone(final String code,final HttpServletResponse response){
    final CompletableFuture<String> async = CompletableFuture.supplyAsync(() -> apiUserService.getWechatPhone(code),taskExecutor);
    ToolClient.responseJson(async.join(),response);
  }

  @ApiOperation("通过微信登录时返回code=198时跳转到绑定手机号(若手机号还未注册时会自动注册并登录),通过手机号默认会生成账号信息,无需完善个人资料,后期可以在个人页面完善个人信息资料-待删除")
  @ApiImplicitParams({
    @ApiImplicitParam(name = "phone", value = "手机号码", dataType = "String", paramType = "query", required = true),
    @ApiImplicitParam(name = "smsCode", value = "短信验证码", dataType = "String", paramType = "query", required = true),
    @ApiImplicitParam(name = "openId", value = "微信的openId", dataType = "String", paramType = "query", required = true)
  })
  @PostMapping("/bindPhone")
  public void bindPhone(final HttpServletRequest request,final HttpServletResponse response){
    ToolClient.responseJson(apiUserService.bindPhone(new PageFormData(request)),response);
  }
}